package org.shopen.puremvc.view
{
	
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import org.puremvc.as3.utilities.statemachine.StateMachine;
	import org.shopen.puremvc.ApplicationConstants;

	public class StudyMediator extends Mediator
	{
		public static const NAME : String = "StudyMediator";
		
		public function StudyMediator(viewComponent:Object=null)
		{
			super(StudyMediator.NAME, viewComponent);
		}
		
		override public function onRegister():void
		{
			facade.registerMediator(new HeaderControlBarMediator(application.headerControlBar));
			
			addEventListeners();
		}
		
		override public function getMediatorName():String
		{
			return StudyMediator.NAME;
		}
		
		protected virtual function get application() : Study
		{
			return viewComponent as Study;
		}
		
		override public function listNotificationInterests():Array
		{
			return [StateMachine.CHANGED];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var noteName : String = notification.getName();
			switch (noteName)
			{
				case StateMachine.CHANGED:
					handleStateChanged(notification);
					break;
			}
		}
		
		private function addEventListeners() : void
		{
			application.addEventListener(Study.INTROWINDOW_CREATED, onIntroWindowCreated);
			application.addEventListener(Study.READING_ACCORDION_CREATED, onReadingCreated);
			application.addEventListener(Study.TESTING_PANEL_CREATED, onTestingCreated);
		}
		
		private function onIntroWindowCreated(event : Event) : void
		{
			checkForMediator(Study.INTROWINDOW, application.introWindow);
		}
		
		private function onReadingCreated(event : Event) : void
		{
			checkForMediator(Study.READING, application.contentAccordion);
		}
		
		private function onTestingCreated(event : Event) : void
		{
			checkForMediator(Study.TESTING, application.testingPanel);
		}
		
		protected function checkForMediator(childSelector : Number, child : Object):void
        {
            switch (childSelector)
            {
                 case Study.INTROWINDOW:
                      if (!facade.hasMediator(IntroWindowMediator.NAME))
                         facade.registerMediator(new IntroWindowMediator(child));
                      break;
                 case Study.READING:
                 	  if (!facade.hasMediator(ContentAccordionMediator.NAME))
                      	facade.registerMediator(new ContentAccordionMediator(child));
                 	  break;
                 case Study.TESTING:
                 	  if (!facade.hasMediator(TestingPanelMediator.NAME))
                      	facade.registerMediator(new TestingPanelMediator(child));
                 	  break;                 
            }
        }
        
        protected function handleStateChanged(notification : INotification) : void
        {
        	var type : String = notification.getType();
        	switch (type)
        	{
        		case ApplicationConstants.STATE_WELCOMING:
        			application.viewState = Study.VIEWSTATE_WELCOMING;
        			break;
        		case ApplicationConstants.STATE_READING:
        			application.viewState = Study.VIEWSTATE_READING;	             	
        			break;
        		case ApplicationConstants.STATE_TESTING:
        			application.viewState = Study.VIEWSTATE_TESTING;
        			break;
        	}
        }
	}
}